<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>部门表单</title>
<link rel="stylesheet" href="res/layui/css/layui.css">
<script src="res/js/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="res/layui/layui.js"></script>
</head>
<body style="padding: 40px 40px 0px 40px">
	<form class="layui-form" id="department">
		<input type="hidden" id="wareId" name="wareId">
		<input type="hidden" id="wareFactory" name="wareFactory"/>
		<div class="layui-form-item">
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">药品种类：</label>
				<div class="layui-input-block">
					<select name="typeId" lay-verify="">

					</select>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">供应商</label>
			<div class="layui-input-block">
				<select name="applyId" lay-verify="required"  lay-search="">
          			<option value="1">总店提供</option>
        		</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">药品名称</label>
			<div class="layui-input-block">
				<input type="text" id="wareName" name="wareName"
					lay-verify="required" autocomplete="off" placeholder="药品名称"
					class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">单价</label>
			<div class="layui-input-block">
				<input type="text" id="wareMoney" name="wareMoney"
					lay-verify="required" autocomplete="off" placeholder="单价"
					class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">有效期</label>
			<div class="layui-input-block">
				<input type="text" id="wareEndti" name="wareEndti"
					lay-verify="required" autocomplete="off" placeholder="有效期"
					class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">产品规格</label>
			<div class="layui-input-block">
				<input type="text" id="wareNorma" name="wareNorma"
					lay-verify="required" autocomplete="off" placeholder="产品规格"
					class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">计量单位</label>
			<div class="layui-input-block">
				<input type="text" id="measure" name="measure" lay-verify="required"
					autocomplete="off" placeholder="计量单位" class="layui-input">
			</div>
		</div>
		
		<div class="layui-form-item">
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">说明书</label>
				<div class="layui-input-block">
					<textarea placeholder="请输入内容" class="layui-textarea"
						id="WARE_remark" name="WARE_remark"></textarea>
				</div>
			</div>
		</div>
		<input id="plList" type="hidden"/>
		<div class="layui-form-item" style="height:200px;">
			<label class="layui-form-label"></label>
			<input id="tjpl" type="button" class="layui-btn layui-btn-primary" value="编辑配料" ><label class="ui_button ui_button_primary" for="xFile"><div type="button" class="layui-btn" id="test1">上传图片</div><img id="nowimg" style="margin-left:50px; height:200px; display: none;"></label>
			<div id="wrapper" style="display: none;">
				<input name="file" accept="image/png, image/jpeg, image/gif, image/jpg"  id="xFile" type="file" /><br />
				<div id="image-holder"></div>
			</div>
		</div>
		<div class="layui-input-block" style="margin-bottom: 100px;">
			<input class="layui-btn" type="button" onclick="add()" value="立即提交"/>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
		 
	</form>

	<script type="text/javascript">
			
			var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
			var departId;
			$(function (){
				
				departId = GetQueryString("wareId");
				loadType();
				apply();
				if(departId!=null){
					findById(departId);
				}
				$("#tjpl").on("click",function(){
					var pl=$("#plList").val();
					layer.open({
						type : 2//样式
						,
						skin : 'layui-layer-molv'//样式
						,
						area : [ '90%', '90%' ],
						title : "配料管理"//标题
						,
						id : 'pl' //防止重复弹出
						,
						content : "pages/batching.jsp?pl="+pl,
						shade : [ 0.8, '#393D49' ] //显示遮罩
						,
						shadeClose : false//点击也能遮罩层关闭
						,
						anim : 2
					//弹出动画 
					});
				})
				$("#xFile").on('change', function () {
					 
					//获取上传文件的数量
					   var countFiles = $(this)[0].files.length;
					 	
					   var imgPath = $(this)[0].value;
					   var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
					 
					   if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
					       if (typeof (FileReader) != "undefined") {
					 
					           // 循环所有要上传的图片
					           for (var i = 0; i < countFiles; i++) {
					 
					               var reader = new FileReader();
					               reader.onload = function (e) {
					            	   $("#nowimg").css("display","inline");
					            	   $("#nowimg").attr("src",e.target.result);
					               }

					               reader.readAsDataURL($(this)[0].files[i]);
					           }
					       } else {
					    	   
					       }
					   } else {
					       
					   }
					});
				
			});
			
			var form;
			layui.use(['form'], function() {
				form = layui.form;
				//监听提交
				form.on('submit(demo)', function(data){
					
					return false;
				});
				
			});
			function add(){
				
				
				var up=true;
				$("[type=text]").each(function(){
					if(!up){
						return;
					}
					if($(this).val()==''||$(this).val()==null){
						up=false;
					}
				})
				if(up){
					$("#wareFactory").val($("[name=applyId] ").find("option:selected").text())
					if($("#plList").val()==""||$("#plList").val()==null){
						layer.msg('请添加药品的配料',{icon: 5,anim: 6}); 
					}else{
					var data = new FormData($("#department")[0]);
					var loadIndex = layer.load();
						$.ajax({
		                     type : "POST",
		                     url : "ware/addOrUpdate.action?dep="+$("#plList").val(),
		                     data : data,
		                     cache : false,
		                     processData : false,  //  不处理发送的数据，因为data值是Formdata对象，不需要对数据做处理
		                     contentType : false,  //  不设置Content-type请求头
		                     success : function(mes){
		                    	 parent.layer.msg(mes.mes);
		 						layer.close(loadIndex);//加载层关闭  
		 						parent.layer.close(index);
		 						if($("#wareId").val()!=""){
		 							$(".layui-laypage-skip .layui-laypage-btn",window.parent.document).click();//刷新父页面数据表格的当前页
		 						}else{
		 							parent.table.reload('typeId');
		 						}
		                     },
		                     error :function(){ 
		                    	layer.close(loadIndex);//加载层关闭  
		                    	layer.msg("发生未知错误，请稍后再试！");
		                     }
		              })
					}
				}else{
					layer.msg('请将信息填写完整',{icon: 5,anim: 6}); 
				}
				 
				/* $.ajax({
					type: "POST",
					url: "ware/addOrUpdate.action?"+$("#plList").val(),
					data: data,
					async: false,
					error: function(){
						layer.mes("发生未知错误，请稍后再试！");
					},
					success: function(mes){
						parent.layer.msg(mes.mes);
						layer.close(loadIndex);//加载层关闭  
						parent.layer.close(index);
						if($("#wareId").val()!=""){
							$(".layui-laypage-skip .layui-laypage-btn",window.parent.document).click();//刷新父页面数据表格的当前页
						}else{
							parent.table.reload('typeId');
						}
					}
				}); */
			}
			function findById(wareId){
				$.ajax({
					type: "POST",
					url: "ware/findById.action",
					data: {"wareId":wareId},
					async: false,
					error: function(){
						layer.mes("发生未知错误，请稍后再试！");
					},
					success: function(obj){
						$("#wareName").val(obj.wareName);
						$("#wareId").val(obj.wareId);
						$("#wareFactory").val(obj.wareFactory);
						$("#wareMoney").val(obj.wareMoney);
						$("#wareEndti").val(obj.endTi);
						$("#wareNorma").val(obj.norma);
						$("#measure").val(obj.measure);
						$("#WARE_remark").text(obj.WARE_remark);
						$("[name=typeId] option").each(function(){
							if($(this).val()==obj.typeId){
								$(this).attr("selected","selected");
							}
						})
						$("[name=applyId] option").each(function(){
						
							if($(this).val()==obj.applyId){
								$(this).attr("selected","selected");
							}
						})
						if(obj.warePhoto!=null&&obj.warePhoto!=''){
							$("#nowimg").css("display","inline");
							$("#nowimg").attr("src",obj.warePhoto);
						}
						var dep=obj.depList;
						var str=""
						for(var i=0;i<dep.length;i++){
							str+="dosName="+dep[i].dosName+",dosCount="+dep[i].dosCount+",dosId="+dep[i].dosId+"_";
						}
						
						$("#plList").val(str);
					}
				});
			}
			function loadType(){
				$.ajax({
					type: "POST",
					url: "wareType/showAll.action",
					data: null,
					async: false,
					error: function(){
						layer.mes("发生未知错误，请稍后再试！");
					},
					success: function(obj){
						var typeList=obj.data;
						for(var i=0;i<typeList.length;i++){
							var type=typeList[i];
							$("[name=typeId]").append("<option value="+type.typeId+">"+type.typeName+"</option>")
						}
					}
				});
			}
			
			function apply(){
				$.ajax({
					type: "POST",
					url: "apply/showAll.action",
					data: null,
					async: false,
					error: function(){
						layer.mes("发生未知错误，请稍后再试！");
					},
					success: function(obj){
						for(var i=0;i<obj.length;i++){
							$("[name=applyId]").append("<option value="+obj[i].applyId+">"+obj[i].applyName+"</option>");
						}
					}
				});
			}
			
		   /**
			*得到URL栏的参数
			*/
			function GetQueryString(name){ 
			     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			     var r = window.location.search.substr(1).match(reg);
			     if(r!=null)return  unescape(r[2]); return null;
			}
		</script>
</body>
</html>